// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.moc.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.moc.controller.request.ImportEcnReq;
import com.liuxinlong.modules.moc.controller.request.QueryEcnReq;
import com.liuxinlong.modules.moc.service.EcnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ecr管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年10月11日
 */
@RestController
@RequestMapping("/moc/ecr")
public class EcrController {

    @Autowired
    private EcnService ecnService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询ecn信息
     *
     * @param req 请求参数
     * @return ecn信息分页数据
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.ECN_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageEcnList(@RequestBody QueryEcnReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("materialNo", req.getMaterialNo());
        queryParam.put("ecnNo", req.getEcnNo());
        queryParam.put("ecrNo", req.getEcrNo());
        List<Map<String, Object>> dataList = ecnService.pageEcnList(queryParam);
        int total = ecnService.getEcnCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * ecn文件同步
     *
     * @return 成功标识
     */
    @GetMapping("file-sync")
    @LogAnnotation(value = "ecn文件同步", business = LogBusinessConstants.ECN_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp syncEcnFile() {
        int total = ecnService.syncEcnFile();
        BaseResp resp = new BaseResp();
        resp.setRetMessage("ecn文件同步成功！");
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入ecn数据", business = LogBusinessConstants.ECN_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importEcn(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = ecnService.importEcn((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入ECN信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入ecn数据", business = LogBusinessConstants.ECN_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddEcn(@RequestBody ImportEcnReq req) {
        return ecnService.batchAddEcn(req.getDataList());
    }
}
